/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package view;

import controller.CustomerController;
import java.io.Serializable;
import java.util.ArrayList;
import javax.ejb.EJB;

import javax.enterprise.context.Conversation;
import javax.enterprise.context.ConversationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import model.Customer;
import model.Navigation;

/**
 *
 * @author zno
 */
@Named(value = "customerManager")
@ConversationScoped
public class CustomerManager implements Serializable {

    private static final String CUSTOMER_EXISTS = "A customer by that name and password already exists!";
    private static final String LOGIN_SUCCESSFUL = "Login was successful";
    private static final String LOGIN_UNSUCCESSFUL = "Login was unsuccessful";
    private static final String CUSTOMER_CREATION_SUCCESSFUL = "Customer creation was successful";
    private static final String CUSTOMER_LOGGED_OUT = "Customer creation was successful";
    private static final String CUSTOMER_LOGIN_SUCCEEDED = "Customer login succeeded";
    private static final String CUSTOMER_LOGIN_FAILED = "Customer login failed";
    private static final String ADMIN_LOGIN_SUCCEEDED = "Admin login succeeded";
    private static final String ADMIN_LOGIN_FAILED = "Admin login failed";
    private static final String CUSTOMER_LOGOUT_SUCCEEDED = "Customer logout succeeded";
    private static final String CUSTOMER_LOGOUT_FAILED = "Customer logout failed";

    private static final String CUSTOMER_DOESNT_EXIST = "No customer exists by that name and password";

    
    
    // Customer deleted
    // Customer updated
    
    
    
    private String name;
    private String password;
    private int balance;
    
    private boolean adminLogin;
    private boolean customerLogin;
    
    private boolean logout;
    private boolean checkout;
    
    private ArrayList<Customer> customers;
    
    
    private String info = null;
        
    
     @EJB
    private CustomerController controller;

    @Inject
    private Conversation conversation;
    
    public CustomerManager() {
        
    }
    /**
     * Lists the currently registered customers
     */
    public void listCustomers() {
        startConversation();
        setLogout(false);
        customers = controller.listCustomers();
    }
    /**
     * Creates a new customer and displays the outcome status
     */
    public void createCustomer() 
    {
        startConversation();
        setCustomerLogin(false);
        setAdminLogin(false);
       

        if (!controller.isCustomer(getName(), getName())) {
            controller.createCustomer(getName(), getName(),balance);
            
            info = CUSTOMER_CREATION_SUCCESSFUL;
        }
        else {
            info = CUSTOMER_EXISTS;
        }
    }
    /**
     * Deletes a customer
     */
    public void deleteCustomer() 
    {
        startConversation();
        setLogout(false);

        controller.deleteCustomer(getName(), getPassword());
    }
    /**
     * Lets a Customer login to the web shop and displays the outcome status
     */
    public void login () 
    {
        startConversation();
        setCustomerLogin(false);
        setAdminLogin(false);
        
        boolean success = controller.login(getName(),getPassword());
        
        if (success && getName().equalsIgnoreCase("admin")) {
            setCustomerLogin(false);
            setAdminLogin(true);
        }
        else if (success) {
            setCustomerLogin(true);
            setAdminLogin(false);
        }
        Customer c = controller.loginCredentials(getName());
        info = getName()+" "+getPassword()+" "+success+"\n"+
                c.getId()+":"+c.getLoggedIn()+":"+c.getBanned()+":"+
                c.getName()+":"+c.getPassword()+":"+c.getBalance();
    }
    /**
     * Lets a customer log out from the web shop and displays the outcome status
     */
    public void logout () 
    {
        startConversation();
        
        boolean l = controller.logout(getName(), getPassword());
        
        if (l)      info = CUSTOMER_LOGOUT_SUCCEEDED;
        else        info = CUSTOMER_LOGOUT_FAILED;
        
        setLogout(l);
        setCheckout(false);
        
       
    }
    /*
     * Checks out the customer 
     */
    public void checkout () {
        setCheckout(true);
        setLogout(false);
    }
    /**
     * Bans a Cutomer
     * @param c     The Customer
     */
    public void ban (Customer c) 
    {
        startConversation();
        setLogout(false);

        controller.ban(c);
    }
    /**
     * Unbans a Customer
     * @param c     The Customer
     */
    public void unban (Customer c) 
    {
        startConversation();
        setLogout(false);

        controller.unban(c);
    }
    /**
     * Starts the conversation scope
     */
    private void startConversation() 
    {
        if (conversation.isTransient()) {
            conversation.begin();
        }
    }
    /**
     *  Ends the conversation scope
     */
    private void stopConversation() {
        if (!conversation.isTransient()) {
            conversation.end();
        }
    }
    
    
    
    /**
     * 
     * @return customerController
     */
    public CustomerController getController() {
        return controller;
    }
    /**
     * 
     * @param controller a new Customer Controller
     */
    public void setController(CustomerController controller) {
        this.controller = controller;
    }
    /**
     * 
     * @return the current conversation scope
     */
    public Conversation getConversation() {
        return conversation;
    }
    /**
     * 
     * @param conversation a new conversation scope
     */
    public void setConversation(Conversation conversation) {
        this.conversation = conversation;
    }
    /**
     * 
     * @return the currently logged in User Name
     */
    public String getName() {
        return Navigation.getSingletonObject().getName();
    }
    /**
     * 
     * @param name sets the currently logged in User Name
     */
    public void setName(String name) {
        Navigation.getSingletonObject().setName(name);
    }
    /**
     * 
     * @return the currently logged in password
     */
    public String getPassword() {
        return Navigation.getSingletonObject().getPassword();
    }
    /**
     * 
     * @param password sets the logged in password
     */
    public void setPassword(String password) {
        Navigation.getSingletonObject().setPassword(password);
    }
    /**
     * 
     * @return the Status Message
     */
    public String getInfo() {
        return info;
    }
    /**
     * 
     * @param info a new Status Message
     */
    public void setInfo(String info) {
        this.info = info;
    }
    /**
     * 
     * @return status of the adminLogin
     */
    public boolean isAdminLogin() {
        return adminLogin;
    }
    /**
     * 
     * @param adminLogin sets a new status for adminLogin
     */
    public void setAdminLogin(boolean adminLogin) {
        this.adminLogin = adminLogin;
    }
    /**
     * 
     * @return status of the cutomerLogin
     */
    public boolean isCustomerLogin() {
        return customerLogin;
    }
    /**
     * 
     * @param customerLogin sets a new status for customerLogin
     */
    public void setCustomerLogin(boolean customerLogin) {
        this.customerLogin = customerLogin;
    }

    
    /**
     * 
     * @return logout status
     */
    public boolean isLogout() {
        return Navigation.getSingletonObject().isLogout();
    }
    /**
     * 
     * @param logout a new logout status
     */
    public void setLogout(boolean logout) {
        Navigation.getSingletonObject().setLogout(logout);
    }
    /**
     * 
     * @return a list of currently registered customers
     */
    public ArrayList<Customer> getCustomers() {
        return customers;
    }
    /**
     * 
     * @param customers a new list of registered customers
     */
    public void setCustomers(ArrayList<Customer> customers) {
        this.customers = customers;
    }
    /**
     * 
     * @return the current balance of the logged in Customer
     */
    public int getBalance() {
        return balance;
    }
    /**
     * Only used when creating a Customer
     * @param balance sets a new balance for the newly created Customer
     */
    public void setBalance(int balance) {
        this.balance = balance;
    }
    /**
     * 
     * @return the checkout status
     */
    public boolean isCheckout() {
        return Navigation.getSingletonObject().isCheckout();
    }
    /**
     * 
     * @param checkout a new checkout status
     */
    public void setCheckout(boolean checkout) {
        Navigation.getSingletonObject().setCheckout(checkout);
    }
    
}
